package com.formulas.certification.oauth.executor;

import com.formulas.certification.domain.oauth.model.OauthClientDetailsEntity;
import com.formulas.certification.dto.OauthClientDetailsUpdateCmd;
import com.formulas.certification.oauth.extensionpoint.OauthClientDetailsCheckExtPt;
import com.formulas.certification.oauth.extensionpoint.OauthClientDetailsConvertorExtPt;
import com.formulas.component.dto.Response;
import com.formulas.component.extension.BizScenario;
import com.formulas.component.extension.ExtensionExecutor;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @author yiwannuofulasi
 * @date 2023/6/13 11:22
 */
@Component
public class OauthClientDetailsUpdateCmdExe {

    @Resource
    private ExtensionExecutor extensionExecutor;

    public Response execute(OauthClientDetailsUpdateCmd cmd) {
        BizScenario bizScenario = cmd.getBizScenario();
        // trip1: 根据客户端唯一标识校验
        extensionExecutor.execute(OauthClientDetailsCheckExtPt.class,
                bizScenario, extension -> extension.oauthClientDetailsCheck(cmd.getClientId()));
        // trip2: 客户端对象转领域模型
        OauthClientDetailsEntity oauthClientDetailsEntity =
                extensionExecutor.execute(OauthClientDetailsConvertorExtPt.class,
                        bizScenario, extension -> extension.clientToEntity(cmd.getOauthClientDetailsDTO()));
        oauthClientDetailsEntity.setBizScenario(bizScenario);
        // trip3: 调用领域模型更新行为方法
        oauthClientDetailsEntity.updateOauthClientDetails();
        return Response.buildSuccess();
    }
}
